<?php
namespace app\admin\controller;
use app\admin\model\Users;
use app\admin\model\Role;
use app\admin\controller\AdminAuth;
use think\Validate;
use think\Image;
use think\Request;
class RoleController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '角色',
		'module_url'  => '/admin/Role/',
		'module_slug' => 'Users',
		'upload_path' => UPLOAD_PATH,
		'upload_url'  => '/public/uploads/',
		);


    /**
     * [index 获取角色数据列表]
     * @return [type] [description]
     */
    public function index()
    {
        //获取前端参数
        $list =  Role::where('status','>=','0')->order('id', 'ASC')->paginate();
        $this->assign('data',$this->data);
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * [create 创建节点数据页面]
     * @return [type] [description]
     */
    public function create()
    {
    	$this->data['edit_fields'] = array(
			'name' => array('type' => 'text', 'label'     => '角色名称'),
            'type'     => array('type' => 'radio', 'label' => '是否管理','default'=> array(0 => '否', 1 => '是')),
			'status'     => array('type' => 'radio', 'label' => '状态','default'=> array(-1 => '删除', 0 => '禁用', 1 => '正常')),
        );

        //默认值设置
        $item['status'] = '正常';

        $this->assign('item',$item);
        $this->assign('data',$this->data);
        return view();
    }

    /**
     * [add 新增部门数据ACTION，create()页面表单数据提交到这里]
     * @return [type] [description]
     */
    public function add()
    {
        $Role = new Role;
        $data = input('post.');

        $rule = [
            'name|角色名称' => 'require|min:2|unique:Role,name',
        ];
        // 数据验证
        $validate = new Validate($rule);
        $result   = $validate->check($data);
        if(!$result){
            return  $this->error($validate->getError());
        }

        $data['create_time'] = time();
        $data['update_time'] = time();


        if ($id = $Role->validate(true)->insertGetId($data)) {
            return $this->success('角色添加成功',$this->data['module_url'].$id);
        } else {
            return $this->error($Role->getError());
        }
    }



    /**
     * [read 读取节点数据]
     * @param  string $id [用户ID]
     * @return [type]     [description]
     */
    public function read($id='')
    {
        $this->data['edit_fields'] = array(
			'name' => array('type' => 'text', 'label'     => '角色名称'),
            'type'     => array('type' => 'radio', 'label' => '是否管理','default'=> array(0 => '否', 1 => '是')),
			'status'   => array('type' => 'radio', 'label' => '状态','default'=> array(-1 => '删除', 0 => '禁用', 1 => '正常')),
        );

        //默认值设置
        $item = Role::get($id);
        $this->assign('item',$item);
        $this->assign('data',$this->data);
        return view();
    }

    /**
     * [update 更新部门数据，read()提交表单数据到这里]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function update($id)
    {
        $Role = new Role;
        $data = input('post.');

        $preview = $Role->where(array('name'=>$data['name']))->find();


        $rule = [
            'name|角色名称' => 'require|min:2',
        ];
        $msg = [];


        // 数据验证
        $validate = new Validate($rule,$msg);
        $result   = $validate->check($data);
        if(!$result){
            return  $this->error($validate->getError());
        }
        $data['id'] = $id;

        if ($Role->update($data)) {
            return $this->success('角色信息更新成功',$this->data['module_url'].$id);
        } else {
            return  $this->error($Role->getError());
        }
    }

    /**
     * [delete 删除部门数据(伪删除)]
     * @param  [type] $id [表ID]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $Role = new Role;
        $data['id'] = $id;
        $data['status'] = -1;
        if ($Role->update($data)) {
        	$data['error'] = 0;
        	$data['msg'] = '删除成功';
        } else {
        	$data['error'] = 1;
        	$data['msg'] = '删除失败';
        }
        return $data;

        // 真.删除，不想用伪删除，请用这段
        // $Role = Role::get($id);
        // if ($Role) {
        //     $Role->delete();
        //     $data['error'] = 0;
        // 	$data['msg'] = '删除成功';
        // } else {
        // 	$data['error'] = 1;
        // 	$data['msg'] = '删除失败';
        // }
        // return $data;
    }
}